package net.hn.hnms.biz.meeting.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.meeting.domain.MeetingInfo;
import net.hn.hnms.biz.meeting.domain.MeetingPerson;
import net.hn.hnms.biz.meeting.mapper.MeetingInfoMapper;
import net.hn.hnms.biz.meeting.mapper.MeetingPersonMapper;
import net.hn.hnms.biz.meeting.request.MeetingInfoReq;
import net.hn.hnms.biz.meeting.request.MeetingPersonReq;
import net.hn.hnms.biz.meeting.service.MeetingInfoService;
import net.hn.hnms.biz.meeting.vo.MeetingInfoVo;
import net.hn.hnms.biz.meeting.vo.MeetingPersonVo;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.core.utils.DateUtils;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.sys.system.api.RemoteUserService;
import net.hn.hnms.sys.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MeetingInfoServiceImpl extends BaseService<MeetingInfo, MeetingInfoVo> implements MeetingInfoService {

    @Autowired
    private MeetingInfoMapper meetingInfoMapper;

    @Autowired
    private MeetingPersonMapper meetingPersonMapper;

    @Autowired
    private RemoteUserService userService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(MeetingInfoReq meetingInfoReq)  {
        //查询会议时间是否冲突
        List<MeetingInfoVo> meetingInfoVoList = meetingInfoMapper.selectMeetingInfoList(meetingInfoReq);
        if(!DateUtils.checkDate(meetingInfoReq.getStartTime(),meetingInfoReq.getEndTime())){
            throw new HuaNengException(Constants.FAIL, "开始时间和结束时间不成立");
        }else if(meetingInfoVoList.size()!=0){
            throw new HuaNengException(Constants.FAIL, "已有预约中会议,会议冲突");
        } else{
            MeetingInfo insertObj = new MeetingInfo();
            MeetingPerson meetingPerson = null;
            BeanUtil.copyProperties(meetingInfoReq, insertObj);
            int insert = meetingInfoMapper.insert(insertObj);
            int insert1 = 0;
            if(insert > 0){
                List<LoginUser> userList = userService.getUserListByIdList(meetingInfoReq.getUserIds());
                for(LoginUser s :userList){
                    meetingPerson = new MeetingPerson();
                    meetingPerson.setPersonId(s.getUserId().toString());
                    meetingPerson.setMeetingId(insertObj.getMeetingId());
                    meetingPerson.setState("0");
                    meetingPerson.setPersonName(s.getNickname());
                    meetingPerson.setPersonId(s.getUserId().toString());

                    insert1 = meetingPersonMapper.insert(meetingPerson);
                }
            }
            if(insert > 0 && insert1 > 0){
                return 1;
            }
        }
        return 0;
    }

    @Override
    public ResponsePage<MeetingInfoVo> selectPageListSql(PageQuery pageQuery,MeetingInfoReq meetingInfoReq ) {
        Page<MeetingInfoVo> page = meetingInfoMapper.selectPageListSql(pageQuery.build(), meetingInfoReq);
        return new ResponsePage<>(page.getRecords(), page.getTotal());
    }

    @Override
    public void updateDeleteByIds(List<String> idList) {
        meetingInfoMapper.updateDeleteByIds(idList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(MeetingInfoReq meetingInfoReq) {
        MeetingInfo meetingInfo = meetingInfoMapper.selectById(meetingInfoReq.getMeetingId());
        if(meetingInfo.getStatus().equals("2")) {
            throw new HuaNengException(Constants.FAIL, "会议已结束，不允许修改");
        }else {
            List<MeetingInfoVo> meetingInfoVoList = meetingInfoMapper.selectMeetingInfoListForUpd(meetingInfoReq);

            if(!DateUtils.checkDate(meetingInfoReq.getStartTime(),meetingInfoReq.getEndTime())){
                throw new HuaNengException(Constants.FAIL, "开始时间和结束时间不成立");
            }else if(meetingInfoVoList.size()!=0){
                throw new HuaNengException(Constants.FAIL, "已有预约中会议,会议冲突");
            }else {
                MeetingInfo updateObj = new MeetingInfo();
//        MeetingInfo updateObj = meetingInfoMapper.selectById(meetingInfoReq.getMeetingId());
                BeanUtil.copyProperties(meetingInfoReq, updateObj);
                int updateCount = meetingInfoMapper.updateById(updateObj);
                MeetingPerson meetingPerson = null;

                int insert1 = 0;
                if(updateCount > 0){
                    int i = meetingPersonMapper.updateDelByMeetingIds(meetingInfoReq.getMeetingId());

                    List<LoginUser> userList = userService.getUserListByIdList(meetingInfoReq.getUserIds());
//            Set<Long> userIds = userList.stream().map(LoginUser::getUserId).collect(Collectors.toSet());
                    for(LoginUser s :userList){
                        meetingPerson = new MeetingPerson();
                        meetingPerson.setPersonId(s.getUserId().toString());
                        meetingPerson.setMeetingId(updateObj.getMeetingId());
                        meetingPerson.setState("0");
                        meetingPerson.setPersonName(s.getNickname());

                        insert1 = meetingPersonMapper.insert(meetingPerson);
                    }
                }
                if (updateCount == 0) {
                    throw new HuaNengException(Constants.FAIL, "数据不存在");
                }
            }
        }

    }

    @Override
    public void updateFileById(MeetingInfoReq meetingInfoReq) {
        MeetingInfo meetingInfo = meetingInfoMapper.selectById(meetingInfoReq.getMeetingId());
        if(meetingInfo!=null){
            meetingInfo.setFileName(meetingInfoReq.getFileName());
            meetingInfo.setFilePath(meetingInfoReq.getFilePath());
            meetingInfo.setOssId(meetingInfoReq.getOssId());
            meetingInfoMapper.updateById(meetingInfo);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<MeetingPersonVo>  updateMeetingInfoStatusById(MeetingInfoReq meetingInfoReq) {
        MeetingInfo meetingInfo = selectDetailById(meetingInfoReq.getMeetingId());
        List<MeetingPersonVo> meetingPersonVos =null;
        if(meetingInfo!=null){
            meetingInfo.setStatus(meetingInfoReq.getStatus());
            if(meetingInfoReq.getStatus().equals("2")){
                MeetingPersonReq meetingPersonReq = new MeetingPersonReq();
                meetingPersonReq.setMeetingId(meetingInfoReq.getMeetingId());
                meetingPersonVos = meetingPersonMapper.selectListSq(meetingPersonReq);
            }
            int i = meetingInfoMapper.updateById(meetingInfo);

        }else{
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        return meetingPersonVos;
    }

    @Override
    public MeetingInfo selectDetailById(String id) {
        MeetingInfo meetingInfo = meetingInfoMapper.selectById(id);
        if (meetingInfo == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
        return meetingInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMeetingPersonById(MeetingInfoReq meetingInfoReq) {
        //根据会议id 删除参会人
        //在根据会议id 添加参会人
        int i = meetingPersonMapper.updateDelByMeetingIds(meetingInfoReq.getMeetingId());
        List<LoginUser> userList = userService.getUserListByIdList(meetingInfoReq.getUserIds());
        MeetingPerson meetingPerson = null;
        for(LoginUser s :userList){
            meetingPerson = new MeetingPerson();
            meetingPerson.setPersonId(s.getUserId().toString());
            meetingPerson.setMeetingId(meetingInfoReq.getMeetingId());
            meetingPerson.setState("0");
            meetingPerson.setPersonName(s.getNickname());
            meetingPersonMapper.insert(meetingPerson);
        }

    }
}
